﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

using NBearLite;
using Xin478.CSWork.Common;
using Xin478.CSWork.DomainModel;
using Xin478.MySocket;

namespace Xin478.CSWork.Server
{
    public class Remind : ResponseBase
    {
        public void 添加(NetEventArgs e, RemindInfo ri)
        {
            this.correspondent.Send(e, "提醒添加", Databases.MyWork.Insert(MyWork.Reminds)
                .AddColumn(MyWork.Reminds.UserId, ri.UserId)
                .AddColumn(MyWork.Reminds.Detail, ri.Detail)
                .AddColumn(MyWork.Reminds.AddTime, DateTime.Now)
                .AddColumn(MyWork.Reminds.DefiniteDate, ri.DefiniteDate)
                .AddColumn(MyWork.Reminds.DefiniteTime, ri.DefiniteTime)
                .AddColumn(MyWork.Reminds.DelayTime, ri.DelayTime)
                .AddColumn(MyWork.Reminds.Interval,ri.Interval)
                .AddColumn(MyWork.Reminds.PlanType,ri.PlanType)
                .Execute()==1);
        }

        public void 删除(NetEventArgs e, int remindId)
        {
            int userId = this.correspondent.OnlineUser.FindUserId(e.Client);

            if (userId != -1 && Databases.MyWork.Delete(MyWork.Reminds).Where(MyWork.Reminds.UserId == userId && MyWork.Reminds.Id == remindId).Execute() == 1)
                this.correspondent.Send(e, "提醒删除", remindId);
        }

        public void 确认提醒(NetEventArgs e, int remindId)
        {
            int userId = this.correspondent.OnlineUser.FindUserId(e.Client);

            if(userId!=-1) Databases.MyWork.Update( MyWork.Reminds)
                .AddColumn( MyWork.Reminds.LastSureTime, DateTime.Now)
                .Where(MyWork.Reminds.Id == remindId && MyWork.Reminds.UserId == userId)
                .Execute();
        }

        public void 提醒数量(NetEventArgs e, string status)
        {
            int userId = this.correspondent.OnlineUser.FindUserId(e.Client);

            int count=0;
            if (userId != -1)
            {
                DateTime dt = DateTime.Now;
                WhereClip wc = MyWork.Reminds.UserId == userId;

                switch (status)
                {
                    case "过期":
                        wc = wc && MyWork.Reminds.DefiniteDate != null && MyWork.Reminds.DefiniteDate < DateTime.Now;
                        break;
                    case "未发生":
                        wc = wc && MyWork.Reminds.DefiniteDate != null && MyWork.Reminds.DefiniteDate > DateTime.Now;
                        break;
                    case "定期":
                        wc = wc && MyWork.Reminds.DefiniteDate == null;
                        break;
                }
                count=Databases.MyWork.Select(MyWork.Reminds, MyWork.Reminds.Id.Count()).Where(wc).ToScalar<int>();
            }


            this.correspondent.Send(e, "提醒数量", count, status);
        }

        public void 提醒列表(NetEventArgs e, int from, int pageSize, string status)
        {
            int userId = this.correspondent.OnlineUser.FindUserId(e.Client);
            if (userId == -1) return;

            WhereClip wc = MyWork.Reminds.UserId == userId;
            switch (status)
            {
                case "过期":
                    wc = wc && MyWork.Reminds.DefiniteDate != null && MyWork.Reminds.DefiniteDate < DateTime.Now.ToString("yyyy-MM-dd");
                    break;
                case "未发生":
                    wc = wc && MyWork.Reminds.DefiniteDate != null && MyWork.Reminds.DefiniteDate > DateTime.Now.ToString("yyyy-MM-dd");
                    break;
                case "定期":
                    wc = wc && MyWork.Reminds.DefiniteDate == null;
                    break;
            }
            IList<RemindInfo> ris = RemindModel.GetList(Databases.MyWork, from, pageSize, wc, MyWork.Reminds.Id, false);

            this.correspondent.Send(e, "提醒列表", from, ris);
        }

        public void 提醒计划(NetEventArgs e)
        {
            int userId = this.correspondent.OnlineUser.FindUserId(e.Client);
            if (userId == -1) return;

            WhereClip wc =
                MyWork.Reminds.UserId == userId
                && (
                    (MyWork.Reminds.DefiniteDate != null
                        && MyWork.Reminds.LastSureTime == null
                        && MyWork.Reminds.DefiniteDate == DateTime.Now.ToString("yyyy-MM-dd")
                    )
                    ||
                    (MyWork.Reminds.DefiniteDate == null
                        && (MyWork.Reminds.PlanType == DateTime.Today.Day || MyWork.Reminds.PlanType == 0 || MyWork.Reminds.PlanType == -(int)Enum.Parse(typeof(DayOfWeek), DateTime.Today.DayOfWeek.ToString()))
                        && (MyWork.Reminds.LastSureTime == null || MyWork.Reminds.LastSureTime < DateTime.Now.Date.ToString("yyyy-MM-dd"))
                    )
                )
                && (MyWork.Reminds.DefiniteTime == -1
                    || (MyWork.Reminds.DefiniteTime >= DateTime.Now.Hour * 100 + DateTime.Now.Minute)
                    || (MyWork.Reminds.DelayTime > 0 && MyWork.Reminds.DefiniteTime >= DateTime.Now.Hour * 100 + DateTime.Now.Minute - MyWork.Reminds.DelayTime * 100)
                    );

            IList<RemindInfo> ris = Databases.MyWork.Select(MyWork.Reminds).Where(wc).ToList<RemindInfo>();
            this.correspondent.Send(e, "提醒计划", ris);
        }
    }
}
